﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace PSE.AuditLogging.Common.Service
{
    public class ServiceClientProxyFactory<T> : IDisposable
    {
        private ChannelFactory<T> _channel;

        public ServiceClientProxyFactory(string address)
        {
            _channel = new ChannelFactory<T>(CreateNetPipeBinding(), new EndpointAddress(address));
            _channel.Open();
        }

        private NetNamedPipeBinding CreateNetPipeBinding()
        {
            NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            binding.MaxBufferPoolSize = 122880;
            binding.MaxBufferSize = 2147483647;
            binding.MaxReceivedMessageSize = 2147483647;

            return binding;
        }

        public T CreateProxy()
        {
            return _channel.CreateChannel();
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (_channel != null)
                _channel.Close();
        }

        #endregion
    }
}
